<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 订单
 */

namespace app\api\controller\user\v1_0;


use think\Db;
class UserOrders extends Common
{
    protected $noNeedRight = ['test'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 列表
     */    
    public function index(){
        $param = request()->param();
        $page = empty(request()->param('pege')) ? 1 : request()->param('pege');
        $limit = empty(request()->param('limit')) ? 10 : request()->param('limit');
        $where = [];
        if($param['state'] != '-1'){
            $where['state'] = $param['state'];
        }
        $list = (new \app\common\model\UserOrders())
                ->page($page)
                ->limit($limit)
                ->field(['id','addtime','state','num','money'])
                ->append(['state_text','addtime_text','goods'])
                ->hidden(['addtime'])
                ->order('id desc')
                ->where(['user_id' => $this->UserInfo['id']])
                ->where($where)->select();
        return self::successResult("成功",$list);
    }
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 详情
     */ 
    public function info(){
        $id = request()->param('id');
        $list = (new \app\common\model\UserOrders())   
                ->field(['id','addtime','state','num','money','on'])
                ->append(['state_text','addtime_text','goods_info'])
                ->hidden(['addtime'])
                ->where(['id' => $id])->find();
        return self::successResult("成功",$list);
    }
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 取消订单
     */ 
    public function cancel(){
        $id = request()->param('id');
    
        Db::startTrans();
        try{
            $message = "订单创建失败,请重试";
            $process = true;

            $row = (new \app\common\model\UserOrders())->where(['id' => $id])->update(['state' => 6]);
            if(!$row){
                $message = "订单取消失败,请重试";
                $process = false;
            }
            $order_goods = (new \app\common\model\UserOrdersGoods())->where(['order_id' => $id])->select();
            foreach ($order_goods as $key => $value) {
                $goods = (new \app\common\model\GoodsSku())->where(['id' => $value['goods_sku_id']])->find();    
                $row = (new \app\common\model\GoodsSku())->where(['id' => $value['goods_sku_id'],'stock' => $goods['stock']])->update([
                    'stock' => $goods['stock']+$value['num']
                ]);
                if(!$row){
                    $message = "订单创建失败,请重试增加商品库存";
                    $process = false;
                }
            }
            
            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
         } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }
        if ($process !== false) {
            return self::successResult("成功");
        } else {
            return self::errorResult($message);
        }
    }
    

}
